///网络请求的状态

enum ResourceState { loading, failed, success }

class Resource<T> {
  final T? _data;
  final ResourceState state;
  final dynamic error;
  Resource._({required this.state, this.error, T? data}): this._data = data;

  factory Resource.failed(dynamic error) {
    return Resource._(state: ResourceState.failed, error: error);
  }

  factory Resource.success(T data) {
    return Resource._(state: ResourceState.success, data: data);
  }

  factory Resource.loading() {
    return Resource._(state: ResourceState.loading);
  }

  T get data{
    if(!isSuccess){
        throw "当前资源状态isSuccess:false,不能使用data属性";
    }
    return _data!;
  }

  bool get isLoading => state == ResourceState.loading;
  bool get isSuccess => state == ResourceState.success;
  bool get isFailed => state == ResourceState.failed;
}
